package cn.bdqn.pn.controller.base;

import cn.bdqn.pn.params.comment.CommentParams;
import cn.bdqn.pn.pojo.base.Comment;
import cn.bdqn.pn.pojo.base.Img;
import cn.bdqn.pn.pojo.commodity.Commodity;
import cn.bdqn.pn.pojo.user.User;
import cn.bdqn.pn.service.base.CommentService;
import cn.bdqn.pn.service.commodity.CommodityService;
import cn.bdqn.pn.service.user.UserService;
/*import cn.bdqn.pn.utils.FastDFSClient;*/
import cn.bdqn.pn.vo.base.CommentVo;
import com.alibaba.fastjson.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 评论
 */
@Controller
@RequestMapping("/Comment")
public class CommentController {


    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommodityService commodityService;


    /**
     * 商品跳入评论页面
     */
    @RequestMapping(value = "/tiao")
    public String jinru(Model model,Integer commodityId){
        Commodity commodity=commodityService.getCommodityById(commodityId);
        String commodityName=commodity.getCommodityName();      //评论名
        System.out.println("评论名："+commodityName);
        model.addAttribute("commodityName02",commodityName);
        return "feedback";
    }



    /**
     * 进入评论页面
     * @return
     */
    @RequestMapping(value = "/show03")
    public String ha03(){
        return "feedback";
    }

    /**
     * 条件分页查
     * @return
     */
    @RequestMapping("/getCommentList")
    public String getCommentList(Model model, CommentParams commentParams,@Param("pageNo") Integer pageNo, @Param("size") Integer size){
        //根据商品名获取商品id，把商品id传入参数
        if(commentParams.getCommodityName()==null||commentParams.getCommodityName().equals("")){
            commentParams.setCommodityId(null);
        }else{
            if(commodityService.findByCommodityName(commentParams.getCommodityName())==null){
                commentParams.setCommodityId(null);
            }else{
                Integer commId = commodityService.findByCommodityName(commentParams.getCommodityName()).getCommodityId();       //根据商品名获取商品id
                commentParams.setCommodityId(commId);
            }
        }
        Page<Comment> comments=commentService.getComments(commentParams,pageNo,size);
        List<CommentVo> commentVoList=new ArrayList<CommentVo>();  //返回对象集合
        CommentVo commentVo;    //返回对象
        //获取评论集合
        List<Comment> commentList=comments.getContent();

        for(Comment com:commentList){
            Integer commentid=com.getCommentid();   //评论id
            System.out.println("包装用户表中存在该用户id："+com.getUserid().intValue());
            User user=userService.selectUserDetails(com.getUserid().intValue());    //获取用户对象
            String userName=user.getNickName(); //获取用户名
            Commodity commodity=commodityService.getCommodityById(com.getCommodityid().intValue());     //获取商品对象
            String commodityName=commodity.getCommodityName();      //获取商品名
            String content=com.getContent();    //评论内容
            Date commenttime=com.getCommenttime();      //评论时间
            Integer likecount=com.getLikecount().intValue();       //点赞量
            Integer status=com.getStatus();    //状态
            Integer imageId=com.getImgid();     //图片id

            commentVo=new CommentVo();
            commentVo.setCommentid(commentid);
            commentVo.setUserId(com.getUserid().intValue());
            commentVo.setUserName(userName);
            commentVo.setCommodityId(com.getCommodityid().intValue());
            commentVo.setCommodityName(commodityName);
            commentVo.setContent(content);
            commentVo.setCommenttime(commenttime);
            commentVo.setLikecount(likecount);
            commentVo.setStatus(status);
            commentVo.setImgid(imageId);

            commentVoList.add(commentVo);

        }


        model.addAttribute("comments",commentVoList);
        model.addAttribute("totalElements",comments.getTotalElements());     //数据总数
        model.addAttribute("number",comments.getNumber());      //当前页数

        model.addAttribute("commodityName",commentParams.getCommodityName());
        model.addAttribute("startDate",commentParams.getStartDate());
        model.addAttribute("endDate",commentParams.getEndDate());
        model.addAttribute("imgId",commentParams.getImgId());


        /*System.out.println(JSON.toJSONString(comments,true));*/
        return "feedback";


    }


    /**
     * 查询子评论
     */
    @ResponseBody
    @RequestMapping("/getZiCommentList")
    public List<CommentVo> getZiCommentList(Model model, CommentParams commentParams,@Param("pageNo") Integer pageNo, @Param("size") Integer size){
        Page<Comment> comments=commentService.getComments(commentParams,pageNo,size);

        List<CommentVo> commentVoList=new ArrayList<CommentVo>();  //返回对象集合
        CommentVo commentVo;    //返回对象
        //获取评论集合
        List<Comment> commentList=comments.getContent();

        for(Comment com:commentList){
            Integer commentid=com.getCommentid();   //评论id
            System.out.println("包装用户表中存在该用户id："+com.getUserid().intValue());
            User user=userService.selectUserDetails(com.getUserid().intValue());    //获取用户对象
            String userName=user.getNickName(); //获取用户名

            Commodity commodity=commodityService.getCommodityById(com.getCommodityid().intValue());     //获取商品对象
            String commodityName=commodity.getCommodityName();      //获取商品对象
            String content=com.getContent();    //评论内容
            Date commenttime=com.getCommenttime();      //评论时间
            Integer likecount=com.getLikecount().intValue();       //点赞量
            Integer status=com.getStatus();    //状态
            Integer imageId=com.getImgid();     //图片id

            commentVo=new CommentVo();
            commentVo.setCommentid(commentid);
            commentVo.setUserId(com.getUserid().intValue());
            commentVo.setUserName(userName);
            commentVo.setCommodityId(com.getCommodityid().intValue());
            commentVo.setCommodityName(commodityName);
            commentVo.setContent(content);
            commentVo.setCommenttime(commenttime);
            commentVo.setLikecount(likecount);
            commentVo.setStatus(status);
            commentVo.setImgid(imageId);

            commentVoList.add(commentVo);
        }


        /*model.addAttribute("comments",comments);*/




        /*System.out.println(comments);
        System.out.println(JSON.toJSONString(comments,true));*/
        return commentVoList;

    }



    /**
     *  修改状态（修改状态1前后台可见2前台不可见）
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByStatus")
    public Boolean updateByStatus(Comment comment){
        Boolean flag=commentService.updateByStatus(comment);
        return flag;
    }

    /**
     * 获取图片
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCommentImages")
    public List<String> getCommentImages(Integer id){
        Img img= commentService.getCommentImages(id);
        System.out.println("评论图片："+img);
        String imgg=img.getImgurl();        //图片路径
        List<String> list=new ArrayList<String>();
        String [] a=imgg.split("\\*");  //用*分割存放数组
        for(int i=0;i<a.length;i++){
            list.add(a[i]);
        }
        return list;
    }

    /**
     * 查询商品评价星级占比率
     * @return  等级-数量
     */
    @ResponseBody
    @RequestMapping("/getPriceSumByDay")
    public String getPriceSumByDay(String Name){
        Integer a=null;
        if(Name==null||Name.equals("")){
            a=null;
        }else {
            if(commodityService.findByCommodityName(Name)==null){
                a=null;
            }else {
                a=commodityService.findByCommodityName(Name).getCommodityId();
            }
        }
        Map<String,Integer> maps=commentService.getPriceSumByDay(a);
        Set<String> keys=maps.keySet();
        for(String key:keys){
            System.out.println(key+"***"+maps.get(key));
        }
        System.out.println(JSONArray.toJSONString(commentService.getPriceSumByDay(a)));
        return JSONArray.toJSONString(commentService.getPriceSumByDay(a),true);
    }



    /************************************************************************************************/



    @GetMapping("/")
    public String index(){
        return "upload";
    }


    /**
     * 进入页面
     */
    @ResponseBody
    @PostMapping("/upload")
    public String  singleFileUpload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            System.out.println("请选择要上传的文件");
        }
        try{
            //String path = FastDFSClient.saveFile(file);
            System.out.println("上传成功");
            System.out.println("名称："+file.getOriginalFilename());
            System.out.println("路径：");
        }catch (Exception e){
            System.err.println("上传失败");
            e.printStackTrace();
        }
        return "控制台查看";
    }
}
